<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Listener;

use App\Event\SmsCheck;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Redis\RedisFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;

/**
 * @Listener
 */
#[Listener]
class CheckSmsCodeListener implements ListenerInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            SmsCheck::class,
        ];
    }

    /**
     * @param SmsCheck $event
     */
    public function process(object $event)
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(RedisFactory::class)->get('db1');
        $code = $redis->get('sms:' . $event->type . ':' . $event->mobile);

        if ($code && $code == $event->code) {
            $event->isPass = true;
        } else {
            $event->isPass = false;
        }
    }
}
